package com.example.androidhw01;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	boolean flag = false;
	TextView showTextView;
	int num1=0;
	//int num2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		showTextView = (TextView)findViewById(R.id.showText);
		Button btn0 = (Button)findViewById(R.id.no0);
		Button btn1 = (Button)findViewById(R.id.no1);
		Button btn2 = (Button)findViewById(R.id.no2);
		Button btn3 = (Button)findViewById(R.id.no3);
		Button btn4 = (Button)findViewById(R.id.no4);
		Button btn5 = (Button)findViewById(R.id.no5);
		Button btn6 = (Button)findViewById(R.id.no6);
		Button btn7 = (Button)findViewById(R.id.no7);
		Button btn8 = (Button)findViewById(R.id.no8);
		Button btn9 = (Button)findViewById(R.id.no9);
		Button plus = (Button)findViewById(R.id.plus);
		Button result = (Button)findViewById(R.id.result);
		
		btn0.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==true) showTextView.setText("");
				showTextView.setText(showTextView.getText().toString()+"0");
				flag = false;
			}
		});
		
		btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==true) showTextView.setText("");
				showTextView.setText(showTextView.getText().toString()+"1");
				flag = false;
			}
		});
		
		btn2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==true) showTextView.setText("");
				showTextView.setText(showTextView.getText().toString()+"2");
				flag = false;
			}
		});
		
		btn3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==true) showTextView.setText("");
				showTextView.setText(showTextView.getText().toString()+"3");
				flag = false;
			}
		});
		
		btn4.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==true) showTextView.setText("");
				showTextView.setText(showTextView.getText().toString()+"4");
				flag = false;
			}
		});
		
		btn5.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==true) showTextView.setText("");
				showTextView.setText(showTextView.getText().toString()+"5");
				flag = false;
			}
		});
		
		btn6.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==true) showTextView.setText("");
				showTextView.setText(showTextView.getText().toString()+"6");
				flag = false;
			}
		});
		
		btn7.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==true) showTextView.setText("");
				showTextView.setText(showTextView.getText().toString()+"7");
				flag = false;
			}
		});
		
		btn8.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==true) showTextView.setText("");
				showTextView.setText(showTextView.getText().toString()+"8");
				flag = false;
			}
		});
		
		btn9.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==true) showTextView.setText("");
				showTextView.setText(showTextView.getText().toString()+"9");
				flag = false;
			}
		});
		
		plus.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String strnum = showTextView.getText().toString();
				num1 = Integer.parseInt(strnum);
				flag = true;
			}
		});
		
		result.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String strnum = showTextView.getText().toString();
				num1 += Integer.parseInt(strnum);
				showTextView.setText("");
				showTextView.setText(""+num1);
				num1=0;
				flag = true;
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
